package com.onlineMIS.action.headQ.finance;

import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import com.onlineMIS.ORM.DAO.Response;
import com.onlineMIS.ORM.DAO.headQ.barCodeGentor.ProductBarcodeService;
import com.onlineMIS.ORM.entity.chainS.report.ChainReport;
import com.onlineMIS.ORM.entity.headQ.finance.FinanceBillCorpFlowVO;
import com.onlineMIS.ORM.entity.headQ.finance.FinanceBillPrintVO;
import com.onlineMIS.ORM.entity.headQ.user.UserInfor;
import com.onlineMIS.common.Common_util;
import com.onlineMIS.common.loggerLocal;
import com.onlineMIS.converter.JSONSQLDateConverter;
import com.onlineMIS.converter.JSONUtilDateConverter;
import com.opensymphony.xwork2.ActionContext;

public class FinanceJSONAction extends FinanceAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = -507154282584622240L;
	protected JSONObject jsonObject;
	private JSONArray jsonArray;
	protected Map<String,Object> jsonMap = new HashMap<String, Object>();

	public JSONObject getJsonObject() {
		return jsonObject;
	}
	public void setJsonObject(JSONObject jsonObject) {
		this.jsonObject = jsonObject;
	}
	
	
	public JSONArray getJsonArray() {
		return jsonArray;
	}
	public void setJsonArray(JSONArray jsonArray) {
		this.jsonArray = jsonArray;
	}
	
	
	/**
	 * 查询某一财务账目的具体流水信息
	 * @return
	 * @throws ParseException 
	 */
	@SuppressWarnings("unchecked")
	public String searchFinanceDetails() throws ParseException {
		UserInfor loginUserInfor = (UserInfor)ActionContext.getContext().getSession().get(Common_util.LOGIN_USER);
		loggerLocal.info(loginUserInfor.getUser_name() + " : FinanceJSONAction.searchFinanceDetails");
		
		Map<String,Object> jsonMap = new HashMap<String, Object>();

    	jsonMap = financeService.getCardFlowDetails(formBean);
		
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.registerJsonValueProcessor(java.sql.Date.class, new JSONSQLDateConverter());
		
		jsonObject = ProductBarcodeService.transferProductBarcode(jsonMap, jsonConfig);
		
		return SUCCESS;
	}
	

	
	
	/**
	 * 查询所有财务账目
	 * @return
	 */
	public String searchFinanceCategory() {
		UserInfor loginUserInfor = (UserInfor)ActionContext.getContext().getSession().get(Common_util.LOGIN_USER);
		loggerLocal.info(loginUserInfor.getUser_name() + " : FinanceJSONAction.searchFinanceCategory");
				
		Response response = financeService.searchFinanceCategory(formBean);
		
		if (response.isSuccess()){
			jsonMap = (Map<String, Object>)response.getReturnValue();
		}
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.registerJsonValueProcessor(java.util.Date.class, new JSONUtilDateConverter());  

		try{
			   jsonObject = JSONObject.fromObject(jsonMap,jsonConfig);
		   } catch (Exception e){
				loggerLocal.error(e);
			}

		
		return SUCCESS;
	}
	
	/**
	 * search the finance bills 
	 * @return
	 */
	public String searchFHQBill(){
		UserInfor loginUserInfor = (UserInfor)ActionContext.getContext().getSession().get(Common_util.LOGIN_USER);
		loggerLocal.info(loginUserInfor.getUser_name() + " : FinanceJSONAction.searchFHQBill");
		Response response = new Response();
		
		try {
		     response = financeService.searchFHQBills(formBean,this.getPage(), this.getRows(), this.getSort(), this.getOrder());
		} catch (Exception e) {
			 response.setQuickValue(Response.FAIL, e.getMessage());
			 loggerLocal.error(e);
		}		
		
		if (response.getReturnCode() == Response.SUCCESS){
			jsonMap = (Map)response.getReturnValue();
		try{
			   jsonObject = JSONObject.fromObject(jsonMap);
			} catch (Exception e){		
				loggerLocal.error(e);
			}
		}
		return SUCCESS;
	}
	
	/**
	 * 根据单据号查询客户财务单据前详情
	 * @return
	 */
	public String selectFinanceBill(){
		UserInfor loginUserInfor = (UserInfor)ActionContext.getContext().getSession().get(Common_util.LOGIN_USER);
		loggerLocal.info(loginUserInfor.getUser_name() + " : FinanceJSONAction.searchFHQBill");
		Response response = new Response();
		
		try {
			FinanceBillPrintVO bill = financeService.selectFinanceBillById(formBean.getFinanceBill().getId());
		     jsonObject = JSONObject.fromObject(bill);
		} catch (Exception e) {
			 response.setQuickValue(Response.FAIL, e.getMessage());
			 loggerLocal.error(e);
		}		
		return SUCCESS;
	}
	
	
	/**
	 * 客户财务管理-创建客户财务单据草稿
	 * @return
	 */
	public String saveFHQToDraft(){
		UserInfor loginUserInfor = (UserInfor)ActionContext.getContext().getSession().get(Common_util.LOGIN_USER);
		loggerLocal.info(loginUserInfor.getUser_name() + " : FinanceJSONAction.saveFHQToDraft");
		Response response = new Response();
		
		try {
		    response = financeService.saveFHQToDraft(formBean.getFinanceBill(), formBean.getHeadQCust(),loginUserInfor);
		} catch (Exception e) {
			response.setFail(e.getMessage());
		}

		jsonMap.put("response", response);
		try{
			   jsonObject = JSONObject.fromObject(jsonMap);
			} catch (Exception e){
				loggerLocal.error(e);
			}
		
		return SUCCESS;
		
	}
	
	/**
	 * 总部客户财务-单据过账
	 * 
	 * @return
	 */
	public String postFHQBill() {
		UserInfor loginUserInfor = (UserInfor) ActionContext.getContext().getSession().get(Common_util.LOGIN_USER);
		loggerLocal.info(loginUserInfor.getUser_name() + " : FinanceJSONAction.postFHQBill");
		Response response = new Response();
		try {
			response = financeService.postFHQBill(formBean.getFinanceBill(), loginUserInfor, formBean.getHeadQCust());
		} catch (Exception e) {
			response.setQuickValue(Response.ERROR, e.getMessage());
			loggerLocal.error(e);
		}

		jsonMap.put("response", response);
		try {
			jsonObject = JSONObject.fromObject(jsonMap);
		} catch (Exception e) {
			loggerLocal.error(e);
		}

		return SUCCESS;

	}
	
	/**
	 * 取得客户当前欠款
	 * @return
	 */
	public String getChainAcctFinance(){
		int custId = formBean.getHeadQCust().getId();
		Response response = new Response();
		
		try {
		    response = financeService.getHeadCustFinance(custId);
		} catch (Exception e) {
			response.setQuickValue(Response.FAIL, "获取欠款发生错误，" + e.getMessage());
		}
		
		jsonMap.put("response", response);
		try{
			   jsonObject = JSONObject.fromObject(jsonMap);
			} catch (Exception e){
				loggerLocal.error(e);
			}
		
		return SUCCESS;
	}
	

	
	/**
	 * 客户财务管理-查询客户往来账户
	 * 
	 * @return
	 */
	public String searchAcctFlow() {
		loggerLocal.info("FinanceJSONAction.searchAcctFlow 查询帐户流水");
		Response response = new Response();
		try {
			//前后端对all_record 一致性
			response = financeService.searchAcctFlow(formBean.getSearchStartTime(),formBean.getSearchEndTime(),Common_util.checkAllRecordValue(formBean.getHeadQCust().getId()),formBean.getTagGroups(), false);
			response.getReturnValue();

		} catch (Exception e) {
			response.setQuickValue(Response.FAIL, e.getMessage());
		}

		jsonMap = (Map<String, Object>) response.getReturnValue();
		JsonConfig jsonConfig = new JsonConfig();

		jsonConfig.registerJsonValueProcessor(java.util.Date.class, new JSONUtilDateConverter());
		jsonConfig.registerJsonValueProcessor(java.sql.Date.class, new JSONSQLDateConverter());

		try {
			jsonObject = JSONObject.fromObject(jsonMap, jsonConfig);
		} catch (Exception e) {
			loggerLocal.error(e);
		}

		return SUCCESS;
	}
	
	/**
	 * the action to generate the finance report
	 * @return
	 */
	public String generateFinanceReport(){
		Response response = new Response();
		try {
			ChainReport chainReport = chainReportService.generateFinanceReport(formBean.getChainStore().getChain_id(), formBean.getSearchStartTime(), formBean.getSearchEndTime());
		    response.setReturnValue(chainReport);
		} catch (Exception e) {
			loggerLocal.error(e);
			response.setReturnCode(Response.FAIL);
		}
		try{
			jsonMap.put("report", response.getReturnValue());

			jsonObject = JSONObject.fromObject(jsonMap);
		} catch (Exception e){

			loggerLocal.error(e);
		}
		
		return SUCCESS;
	}
	
	/**
	 * 保存finance cateogry 
	 * @return
	 */
	public String createUpdateFinanceCategory() {
		loggerLocal.info("FinanceJSONAction.createUpdateFinanceCategory");
		UserInfor loginUserInfor = (UserInfor)ActionContext.getContext().getSession().get(Common_util.LOGIN_USER);
		Response response = new Response();
		
		try {
		    response = financeService.createOrUpdateFinanceCategory(formBean.getFc(),loginUserInfor);
		} catch (Exception e) {
			loggerLocal.error(e);
			response.setReturnCode(Response.FAIL);
		}

	    jsonObject = JSONObject.fromObject(response);
		
		return SUCCESS;
	}
	
	
	/**
	 * 汇总当前的财务报表
	 * @return
	 */
	public String generateFinanceRpt(){
		UserInfor loginUserInfor = (UserInfor)ActionContext.getContext().getSession().get(Common_util.LOGIN_USER);
		loggerLocal.info(loginUserInfor.getUser_name() + " : FinanceSupplierJSONAction.generateFinanceRpt");
		Response response = new Response();
		try {
			response = financeService.generateFinanceReport(formBean, formBean.getSearchStartTime(), formBean.getSearchEndTime());

		} catch (Exception e) {
			loggerLocal.error(e);
			response.setReturnCode(Response.FAIL);
		}
		try{
			jsonMap =  (Map<String, Object>)response.getReturnValue();
			jsonObject = JSONObject.fromObject(jsonMap);
		} catch (Exception e){

			loggerLocal.error(e);
		}
		
		return SUCCESS;
	}
	
	
	/**
	 * 查询客户的预存金充值消费记录信息
	 * @return
	 */
	public String selectPrestoreInfo(){
		UserInfor loginUserInfor = (UserInfor)ActionContext.getContext().getSession().get(Common_util.LOGIN_USER);
		loggerLocal.info(loginUserInfor.getUser_name() + " : FinanceSupplierJSONAction.selectPrestoreInfo");
		Response response = new Response();
		try {
			response = financeService.selectPrestoreInfo(formBean, formBean.getSearchStartTime(), formBean.getSearchEndTime());

		} catch (Exception e) {
			loggerLocal.error(e);
			response.setReturnCode(Response.FAIL);
		}
		try{
			jsonMap =  (Map<String, Object>)response.getReturnValue();
			jsonObject = JSONObject.fromObject(jsonMap);
		} catch (Exception e){

			loggerLocal.error(e);
		}
		
		return SUCCESS;
	}
	
	
	/**
	 * 查询机构转账记录
	 *
	 * @return
	 */
	public String searchFinanceBillCorpFlow() {
		UserInfor loginUserInfor = (UserInfor)ActionContext.getContext().getSession().get(Common_util.LOGIN_USER);
		loggerLocal.info(loginUserInfor.getUser_name() + " : FinanceJSONAction.searchFinanceBillCorpFlow");
		
		Response response = new Response();

		try{
			   response = financeService.searchFinanceBillCorpFlow(formBean, this.getPage(), this.getRows(), this.getSort(), this.getOrder());
				jsonMap = (Map<String, Object>)response.getReturnValue();
			   //System.out.println(jsonObject.toString());
		   } catch (Exception e){
				loggerLocal.error(e);
			} finally {
				jsonObject = JSONObject.fromObject(jsonMap);
			}
		
		return SUCCESS;
	}
	
	/**
	 * 根据转账记录ID，查询转账记录
	 *
	 * @return
	 */
	public String selectFBCorpFlowById() {
		UserInfor loginUserInfor = (UserInfor)ActionContext.getContext().getSession().get(Common_util.LOGIN_USER);
		loggerLocal.info(loginUserInfor.getUser_name() + " : FinanceJSONAction.selectFBCorpFlowById");
		try{
			   FinanceBillCorpFlowVO flow = financeService.selectFBCorpFlowById(formBean.getFinanceBillCorpFlow().getId());
			   jsonMap.put("date", flow);
			   
		   } catch (Exception e){
				loggerLocal.error(e);
			}finally {
				jsonObject = JSONObject.fromObject(jsonMap);
			}
		return SUCCESS;
	}
	
	
	/**
	 * 创建机构转账记录 过账
	 * @return
	 */
	public String postFbillCorpFlow() {
		UserInfor loginUserInfor = (UserInfor)ActionContext.getContext().getSession().get(Common_util.LOGIN_USER);
		loggerLocal.info("FinanceJSONAction.createUpdateFbillCoreFlow");
		Response response = new Response();
		
		try {
		    response = financeService.postFBillCorpFlow(formBean.getFinanceBillCorpFlow(),loginUserInfor);
		} catch (Exception e) {
			loggerLocal.error(e);
			response.setFail(e.getMessage());
		}

	    jsonObject = JSONObject.fromObject(response);
		
		return SUCCESS;
	}
	
	/**
	 * 红冲机构转账单据
	 * @return
	 */
	public String cancelFBillCorpFlow() {
		UserInfor loginUserInfor = (UserInfor)ActionContext.getContext().getSession().get(Common_util.LOGIN_USER);
		loggerLocal.info("FinanceJSONAction.cancelFBillCorpFlow");
		Response response = new Response();
		
		try {
		    response = financeService.cancelFbillCorpFlow(formBean.getFinanceBillCorpFlow(),loginUserInfor);
		} catch (Exception e) {
			loggerLocal.error(e);
			response.setFail(e.getMessage());
		}

	    jsonObject = JSONObject.fromObject(response);
		
		return SUCCESS;
	}
	
	/**
	 * 创建修改机构转账记录 草稿
	 * @return
	 */
	public String saveDraftFbillCorpFlow() {
		UserInfor loginUserInfor = (UserInfor)ActionContext.getContext().getSession().get(Common_util.LOGIN_USER);
		loggerLocal.info("FinanceJSONAction.saveDraftFbillCorpFlow");
		Response response = new Response();
		
		try {
		    response = financeService.saveDraftFbillCorpFlow(formBean.getFinanceBillCorpFlow(),loginUserInfor);
		} catch (Exception e) {
			loggerLocal.error(e);
			response.setFail(e.getMessage());
		}

	    jsonObject = JSONObject.fromObject(response);
		
		return SUCCESS;
	}

	

	/**
	 * 修改机构转账记录 
	 * @return
	 */
	public String deleteFbillCorpFlow() {
		UserInfor loginUserInfor = (UserInfor)ActionContext.getContext().getSession().get(Common_util.LOGIN_USER);
		loggerLocal.info("FinanceJSONAction.deleteFbillCoreFlow");
		Response response = new Response();
		
		try {
		    response = financeService.deleteFbillCoreFlow(formBean.getFinanceBillCorpFlow(),loginUserInfor);
		} catch (Exception e) {
			loggerLocal.error(e);
			response.setFail(e.getMessage());
		}

	    jsonObject = JSONObject.fromObject(response);
		
		return SUCCESS;
	}
	
	
	
	/**
	 * 查询总部费用支出记录
	 *
	 * @return
	 */
	public String searchExpense() {
		UserInfor loginUserInfor = (UserInfor)ActionContext.getContext().getSession().get(Common_util.LOGIN_USER);
		loggerLocal.info(loginUserInfor.getUser_name() + " : FinanceJSONAction.searchExpense");
		
		Response response = financeService.searchExpense(formBean, this.getPage(), this.getRows(), this.getSort(), this.getOrder());

		if (response.isSuccess()){
			jsonMap = (Map<String, Object>)response.getReturnValue();
		}

		try{
			   jsonObject = JSONObject.fromObject(jsonMap);			   
		   } catch (Exception e){
				loggerLocal.error(e);
			}
		
		return SUCCESS;
	}
	
	
	/**
	 * 创建总部管理费用 - 单据过账
	 * @return
	 */
	public String postExpense() {
		UserInfor loginUserInfor = (UserInfor)ActionContext.getContext().getSession().get(Common_util.LOGIN_USER);
		loggerLocal.info("FinanceJSONAction.postExpense");
		Response response = new Response();
		
		try {
		    response = financeService.postExpense(formBean.getExpenseHq(),loginUserInfor);
		} catch (Exception e) {
			loggerLocal.error(e);
			response.setFail(e.getMessage());
		}

	    jsonObject = JSONObject.fromObject(response);
		
		return SUCCESS;
	}
	
	
	
	/**
	 * 红冲总部费用明细
	 * @return
	 */
	public String cancelExpense() {
		UserInfor loginUserInfor = (UserInfor)ActionContext.getContext().getSession().get(Common_util.LOGIN_USER);
		loggerLocal.info("FinanceJSONAction.cancelFBillCorpFlow");
		Response response = new Response();
		
		try {
		    response = financeService.cancelExpense(formBean.getExpenseHq(),loginUserInfor);
		} catch (Exception e) {
			loggerLocal.error(e);
			response.setFail(e.getMessage());
		}

	    jsonObject = JSONObject.fromObject(response);
		
		return SUCCESS;
	}
	
	
	/**
	 * 创建修改总部费用记录 草稿
	 * @return
	 */
	public String saveDraftExpense() {
		UserInfor loginUserInfor = (UserInfor)ActionContext.getContext().getSession().get(Common_util.LOGIN_USER);
		loggerLocal.info("FinanceJSONAction.saveDraftExpense");
		Response response = new Response();
		
		try {
		    response = financeService.saveDraftExpense(formBean.getExpenseHq(),loginUserInfor);
		} catch (Exception e) {
			loggerLocal.error(e);
			response.setFail(e.getMessage());
		}

	    jsonObject = JSONObject.fromObject(response);
		
		return SUCCESS;
	}

	

	/**
	 * 删除总部费用支出
	 * @return
	 */
	public String deleteExpense() {
		UserInfor loginUserInfor = (UserInfor)ActionContext.getContext().getSession().get(Common_util.LOGIN_USER);
		loggerLocal.info("FinanceJSONAction.deleteExpense");
		Response response = new Response();
		
		try {
		    response = financeService.deleteExpense(formBean.getExpenseHq(),loginUserInfor);
		} catch (Exception e) {
			loggerLocal.error(e);
			response.setFail(e.getMessage());
		}

	    jsonObject = JSONObject.fromObject(response);
		
		return SUCCESS;
	}
	
	/**
	 * 复制总部费用支出
	 * @return
	 */
	public String copyExpense() {
		UserInfor loginUserInfor = (UserInfor)ActionContext.getContext().getSession().get(Common_util.LOGIN_USER);
		loggerLocal.info("FinanceJSONAction.copyExpense");
		Response response = new Response();
		
		try {
		    response = financeService.copyExpense(formBean.getExpenseHq(),loginUserInfor);
		} catch (Exception e) {
			loggerLocal.error(e);
			response.setFail(e.getMessage());
		}

	    jsonObject = JSONObject.fromObject(response);
		
		return SUCCESS;
	}
	
	/**
	 * 修改客户财务备注单据
	 * @return
	 */
	public String updateFinanceBillComment() {
		UserInfor loginUserInfor = (UserInfor)ActionContext.getContext().getSession().get(Common_util.LOGIN_USER);
		loggerLocal.info("FinanceJSONAction.updateFinanceBillComment");
		Response response = new Response();
		
		try {
		    response = financeService.updateFinanceBillComment(formBean.getFinanceBill(),loginUserInfor);
		} catch (Exception e) {
			loggerLocal.error(e);
			response.setFail(e.getMessage());
		}

	    jsonObject = JSONObject.fromObject(response);
		
		return SUCCESS;
	}
	
	
}
